<template>
  <div class="contentTag">
    <el-card>
      <!-- 头部区域 -->
      <header-search>
        <template #search>
          <span>标签名称</span>
          <el-input
            v-model="queryData.tagname"
            placeholder="请输入标签名称"
            style="width: 220px"
            size="small"
          />
          <el-button
            icon="el-icon-search"
            type="primary"
            size="small"
            @click="searchList"
          >搜索</el-button>
          <el-button icon="el-icon-refresh" size="small" @click="btnReset">重置</el-button>
        </template>
        <template #addContent>
          <el-button
            icon="el-icon-plus"
            type="primary"
            size="small"
            @click="openDialog"
          >新增</el-button>
          <!-- 导出组件 -->
          <export-upload type="tag" />
        </template>
      </header-search>
      <!-- 表格 -->
      <!-- 列表 -->
      <el-table :data="list" border style="width: 100%" class="articleList">
        <el-table-column fixed label="#" width="80px" type="index" :index="indexMethod" />
        <el-table-column fixed prop="id" label="分类ID" />
        <el-table-column prop="tagname" label="标签名称" />
        <el-table-column label="操作">
          <template #default="{ row }">
            <el-button icon="el-icon-edit" type="primary" size="small" @click="editTag(row.id)" />
            <el-button icon="el-icon-delete" type="danger" size="small" @click="delTag(row.id)" />
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页器 -->
      <el-pagination
        :current-page="queryData.page"
        :page-sizes="[10, 20, 30, 40]"
        :page-size="queryData.limit"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        background
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </el-card>
    <!-- 弹框 -->
    <tag-dialog ref="addTagDialog" @updata="initData" />
  </div>
</template>
<script>
import { getTagListApi, delTagApi } from '@/api/contnet/tag'
import TagDialog from './components/tag-dialog.vue'
export default {
  name: 'ArticleTag',
  components: {
    TagDialog
  },
  data() {
    return {
      // 渲染表格
      list: [],
      // 请求数据
      queryData: {
        tagname: '',
        page: 1,
        limit: 10
      },
      // 列表总数
      total: null
    }
  },
  created() {
    this.initData()
  },
  methods: {
    // 数据初始化
    async initData() {
      const { data: res } = await getTagListApi(this.queryData)
      this.list = res.data
      this.total = res.count
    },
    // 处列表理序号
    indexMethod(index) {
      return (this.queryData.page - 1) * this.queryData.limit + index + 1
    },
    // 分页器 => 每页显示条数
    handleSizeChange(val) {
      this.queryData.limit = val
      this.queryData.page = 1
      this.initData()
    },
    // 分页器 => 当前页
    handleCurrentChange(val) {
      this.queryData.page = val
      this.initData()
    },
    // 删除标签
    async delTag(id) {
      try {
        await this.$confirm('确定要删除吗', '提示')
      } catch (error) {
        return console.log(error)
      }
      if (this.list.length === 1 && this.queryData.page > 1) {
        this.queryData.page--
      }
      await delTagApi(id)
      this.$message.success('删除成功')
      this.initData()
    },
    // 点击新增打开弹框
    openDialog() {
      this.$refs.addTagDialog.showDialog = true
    },
    // 编辑打开弹框
    editTag(id) {
      this.$refs.addTagDialog.editTagShow(id)
    },
    // 搜索区域
    async searchList() {
      const res = await getTagListApi(this.queryData)
      this.list = res.data.data
      this.total = res.data.count
    },
    // 重置
    btnReset() {
      this.queryData.tagname = ''
      this.initData()
    }
  }
}
</script>
<style lang="scss" scoped>
.contentTag {
  padding: 20px 20px 0 20px;
  .el-pagination {
    margin-top: 20px;
    float: right;
  }
  .articleList {
    .el-button {
      width: 30px;
      height: 30px;
      border-radius: 50%;
      padding: 0 ;
    }
  }
}
</style>
